package com.yunduo.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunduo.common.RetunData.CommonReturn;
import com.yunduo.common.RetunData.statuscode.impl.ResultCode;
import com.yunduo.common.YunDuoConst.GoodTypeParam;
import com.yunduo.entity.Good;
import com.yunduo.entity.GoodType;
import com.yunduo.entity.ShopGoods;
import com.yunduo.entity.vo.TreeData;
import com.yunduo.service.GoodService;
import com.yunduo.service.GoodTypeService;
import com.yunduo.service.ShopGoodsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * list页面控制器
 */
@RestController
@RequestMapping("/flist")
public class ListDataContorller {

    @Resource
    private GoodTypeService goodTypeService;
    @Resource
    private GoodService goodService;
    @Resource
    private ShopGoodsService shopGoodsService;
    //获取所有顶部列表
    @GetMapping("/getAllTopTypes")
    public CommonReturn getAllTopTypes(){
        List<GoodType> list = goodTypeService.list(new LambdaQueryWrapper<GoodType>().eq(GoodType::getParentId, GoodTypeParam.PARENT_ID_DEFAULT));
        return CommonReturn.SUCCESS(ResultCode.SUCCESS).setData("list",list);
    }

    //获取子集类型
    @GetMapping("/getChildrenTypes/{id}")
    public CommonReturn getChildrenTypes(@PathVariable("id") String id){
        List<TreeData> list = getListDataById(new ArrayList<TreeData>(), id);
        return CommonReturn.SUCCESS(ResultCode.SUCCESS).setData("list",list);
    }

    //根据类型id查询商品
    @GetMapping("/getGoodsByType")
    public CommonReturn getGoodsByType(String id){
        GoodType type = goodTypeService.getById(id);
        if(type == null){
            return CommonReturn.ERROR(ResultCode.ERROR);
        }
        LambdaQueryWrapper<Good> qw = new LambdaQueryWrapper<>();
        qw.eq(Good::getType,id);
        List<Good> list = goodService.list(qw);
        Iterator<Good> iterator = list.iterator();
        while (iterator.hasNext()){
            Good next = iterator.next();
            LambdaQueryWrapper<ShopGoods> isDelete = new LambdaQueryWrapper<>();
            isDelete.eq(ShopGoods::getGoodId,next.getId());
            ShopGoods one = shopGoodsService.getOne(isDelete);
            //如果没有售卖并且没有上架删除
            if(one == null || !one.getSale()){
                list.remove(next);
            }
        }
        return CommonReturn.SUCCESS(ResultCode.SUCCESS).setData("list",list);

    }
    //递归查询该分类下所有分类
    private List<TreeData> getListDataById(List<TreeData> list,String id){
        List<GoodType> dataList = goodTypeService.list(new LambdaQueryWrapper<GoodType>().eq(GoodType::getParentId, id));
        dataList.forEach(item->{
            List<GoodType> temp = goodTypeService.list(new LambdaQueryWrapper<GoodType>().eq(GoodType::getParentId, item.getId()));
            if (temp.size() == 0){
                list.add(new TreeData(item.getId(),item.getType(),null));
                return;
            }
            getListDataById(list,item.getId());
        });
        return list;
    }


}

